package main

import "github.com/kataras/iris/v12"

func main() {
	app := iris.Default()
	// iris.Map is an alias of map[string]interface{}
	app.Get("/json", func(ctx iris.Context) {
		ctx.JSON(iris.Map{"message": "hello", "status": iris.StatusOK})
	})

	// Use Secure field to prevent json hijacking.
	// It prepends `"while(1),"` to the body when the data is array.
	app.Get("/json_secure", func(ctx iris.Context) {
		response := []string{"val1", "val2", "val3"}
		options := iris.JSON{Indent: "", Secure: true}
		ctx.JSON(response, options)

		// Will output: while(1);["val1","val2","val3"]
	})

	// Use ASCII field to generate ASCII-only JSON
	// with escaped non-ASCII characters.
	app.Get("/json_ascii", func(ctx iris.Context) {
		response := iris.Map{"lang": "GO-虹膜", "tag": "<br>"}
		options := iris.JSON{Indent: "    ", ASCII: true}
		ctx.JSON(response, options)

		/* Will output:
		   {
		       "lang": "GO-\u8679\u819c",
		       "tag": "\u003cbr\u003e"
		   }
		*/
	})

	app.Get("/jsonp", func(ctx iris.Context) {
		ctx.JSONP(iris.Map{"hello": "jsonp"}, iris.JSONP{Callback: "callbackName"})
	})

	app.Listen(":8080", iris.WithOptimizations)
}
